package sudroid.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import sudroid.LogUtils;

public class HttpUtil
{
  public static final String BOUNDARY = "7d4a6d158c9";
  private static final int DEFAULT_CONNECT_TIME_OUT = 15000;
  private static final int DEFAULT_READ_TIME_OUT = 10000;
  public static final String END_MP_BOUNDARY = "--7d4a6d158c9--";
  public static final String MP_BOUNDARY = "--7d4a6d158c9";
  public static final String MULTIPART_FORM_DATA = "multipart/form-data";
  public static final int PROXY_FLAG_3GWAP = 3;
  public static final int PROXY_FLAG_777 = 4;
  public static final int PROXY_FLAG_CMWAP = 1;
  public static final int PROXY_FLAG_NULL = 0;
  public static final int PROXY_FLAG_UNIWAP = 2;
  private static final String USER_AGENT = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091111 Gentoo Firefox/3.5.4";
  private static HttpUtil networkWrapper;
  private int connectTimeout = 15000;
  private int proxyFlag = 0;
  private int readTimeout = 10000;
  private boolean usingGZIP;
  private boolean usingRedirect;
  private boolean usingTimeOut = 1;

  private static HttpResponse ReadResponse(HttpURLConnection paramHttpURLConnection, HttpResponse paramHttpResponse)
    throws IOException
  {
    String str1 = paramHttpURLConnection.getContentEncoding();
    paramHttpResponse.encoding = str1;
    int i = paramHttpURLConnection.getContentLength();
    paramHttpResponse.length = i;
    String str2 = paramHttpURLConnection.getContentType();
    paramHttpResponse.contentType = str2;
    long l = paramHttpURLConnection.getDate();
    paramHttpResponse.sentDate = l;
    int j = paramHttpURLConnection.getResponseCode();
    paramHttpResponse.code = j;
    String str3 = paramHttpURLConnection.getResponseMessage();
    paramHttpResponse.message = str3;
    byte[] arrayOfByte2;
    int k;
    int m;
    if (paramHttpResponse.length > 0)
    {
      byte[] arrayOfByte1 = new byte[paramHttpResponse.length];
      paramHttpResponse.datas = arrayOfByte1;
      arrayOfByte2 = new byte[1024];
      k = 0;
      localInputStream = buildInputStream(paramHttpURLConnection, paramHttpResponse);
      m = localInputStream.read(arrayOfByte2);
      if (m == -1)
        localInputStream.close();
    }
    do
    {
      LogUtils.d(paramHttpResponse.dump());
      return paramHttpResponse;
      byte[] arrayOfByte3 = paramHttpResponse.datas;
      System.arraycopy(arrayOfByte2, 0, arrayOfByte3, k, m);
      k += m;
      break;
    }
    while (paramHttpResponse.length != -1);
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(10240);
    InputStream localInputStream = buildInputStream(paramHttpURLConnection, paramHttpResponse);
    while (true)
    {
      int n = (byte)localInputStream.read();
      if (n == -1)
      {
        localInputStream.close();
        localByteArrayOutputStream.flush();
        byte[] arrayOfByte4 = localByteArrayOutputStream.toByteArray();
        paramHttpResponse.datas = arrayOfByte4;
        localByteArrayOutputStream.close();
        break;
      }
      localByteArrayOutputStream.write(n);
    }
  }

  private HttpResponse basicRequest(String paramString1, Map<String, String> paramMap1, Map<String, String> paramMap2, String paramString2)
    throws IOException, MalformedURLException, ProtocolException
  {
    int i = this.proxyFlag;
    HttpURLConnection localHttpURLConnection = buildProxyConnection(paramString1, i);
    localHttpURLConnection.setDoOutput(1);
    localHttpURLConnection.setRequestMethod(paramString2);
    localHttpURLConnection.setUseCaches(0);
    boolean bool = this.usingRedirect;
    localHttpURLConnection.setInstanceFollowRedirects(bool);
    localHttpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    localHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091111 Gentoo Firefox/3.5.4");
    Iterator localIterator;
    if ((paramMap2 != null) && (paramMap2.size() != 0))
      localIterator = paramMap2.entrySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        if (this.usingGZIP)
          localHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate");
        setTimeouts(localHttpURLConnection);
        PrintStream localPrintStream = System.out;
        String str1 = "url:" + paramString1;
        localPrintStream.println(str1);
        localHttpURLConnection.connect();
        sendData(localHttpURLConnection, paramMap1);
        HttpResponse localHttpResponse1 = new HttpResponse();
        HttpResponse localHttpResponse2 = ReadResponse(localHttpURLConnection, localHttpResponse1);
        localHttpURLConnection.disconnect();
        return localHttpResponse1;
      }
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str2 = (String)localEntry.getKey();
      String str3 = (String)localEntry.getValue();
      localHttpURLConnection.addRequestProperty(str2, str3);
    }
  }

  private static InputStream buildInputStream(HttpURLConnection paramHttpURLConnection, HttpResponse paramHttpResponse)
    throws IOException
  {
    BufferedInputStream localBufferedInputStream;
    if (isGZIPInput(paramHttpResponse.encoding))
    {
      InputStream localInputStream1 = paramHttpURLConnection.getInputStream();
      localBufferedInputStream = new BufferedInputStream(localInputStream1);
    }
    InputStream localInputStream2;
    for (Object localObject = new GZIPInputStream(localBufferedInputStream); ; localObject = new BufferedInputStream(localInputStream2))
    {
      return localObject;
      localInputStream2 = paramHttpURLConnection.getInputStream();
    }
  }

  private static HttpURLConnection buildProxyConnection(String paramString, int paramInt)
    throws IOException, MalformedURLException
  {
    switch (paramInt)
    {
    default:
    case 1:
    case 2:
    case 3:
    }
    Proxy localProxy;
    for (HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(paramString).openConnection(); ; localHttpURLConnection = (HttpURLConnection)new URL(paramString).openConnection(localProxy))
    {
      return localHttpURLConnection;
      Proxy.Type localType = Proxy.Type.HTTP;
      InetSocketAddress localInetSocketAddress = new InetSocketAddress("10.0.0.172", 80);
      localProxy = new Proxy(localType, localInetSocketAddress);
    }
  }

  public static HttpUtil getInstance()
  {
    if (networkWrapper == null)
      networkWrapper = new HttpUtil();
    return networkWrapper;
  }

  private static boolean isGZIPInput(String paramString)
  {
    if ((paramString != null) && (paramString.toLowerCase().indexOf("gzip") > -1));
    for (int i = 1; ; i = 0)
      return i;
  }

  private void sendData(HttpURLConnection paramHttpURLConnection, Map<String, String> paramMap)
    throws IOException
  {
    GZIPOutputStream localGZIPOutputStream;
    if ((paramMap != null) && (paramMap.size() > 0))
    {
      if (!this.usingGZIP)
        break label114;
      OutputStream localOutputStream1 = paramHttpURLConnection.getOutputStream();
      BufferedOutputStream localBufferedOutputStream1 = new BufferedOutputStream(localOutputStream1);
      localGZIPOutputStream = new GZIPOutputStream(localBufferedOutputStream1);
    }
    int i;
    StringBuilder localStringBuilder1;
    Iterator localIterator;
    label114: BufferedOutputStream localBufferedOutputStream2;
    for (DataOutputStream localDataOutputStream = new DataOutputStream(localGZIPOutputStream); ; localDataOutputStream = new DataOutputStream(localBufferedOutputStream2))
    {
      i = 1;
      localStringBuilder1 = new StringBuilder(10);
      localIterator = paramMap.entrySet().iterator();
      if (localIterator.hasNext())
        break;
      String str1 = localStringBuilder1.toString();
      localDataOutputStream.writeBytes(str1);
      localDataOutputStream.close();
      return;
      OutputStream localOutputStream2 = paramHttpURLConnection.getOutputStream();
      localBufferedOutputStream2 = new BufferedOutputStream(localOutputStream2);
    }
    Map.Entry localEntry = (Map.Entry)localIterator.next();
    String str2 = (String)localEntry.getKey();
    String str3 = (String)localEntry.getValue();
    if (i != 0)
      i = 0;
    while (true)
    {
      PrintStream localPrintStream = System.out;
      String str4 = "key:" + str2 + "\tvalue:" + str3;
      localPrintStream.println(str4);
      StringBuilder localStringBuilder2 = localStringBuilder1.append(str2).append("=");
      String str5 = URLEncoder.encode(str3, "UTF-8");
      StringBuilder localStringBuilder3 = localStringBuilder2.append(str5);
      break;
      StringBuilder localStringBuilder4 = localStringBuilder1.append("&");
    }
  }

  private void setTimeouts(HttpURLConnection paramHttpURLConnection)
  {
    if (this.usingTimeOut)
    {
      int i = this.connectTimeout;
      paramHttpURLConnection.setConnectTimeout(i);
      int j = this.readTimeout;
      paramHttpURLConnection.setReadTimeout(j);
    }
  }

  public HttpResponse delete(String paramString, Map<String, String> paramMap)
    throws IOException
  {
    return basicRequest(paramString, null, paramMap, "DELETE");
  }

  public HttpResponse delete(String paramString, Map<String, String> paramMap1, Map<String, String> paramMap2)
    throws IOException
  {
    return basicRequest(paramString, paramMap1, paramMap2, "DELETE");
  }

  public HttpResponse get(String paramString)
    throws IOException
  {
    return get(paramString, null);
  }

  public HttpResponse get(String paramString, Map<String, String> paramMap)
    throws IOException
  {
    int i = this.proxyFlag;
    HttpURLConnection localHttpURLConnection = buildProxyConnection(paramString, i);
    localHttpURLConnection.setRequestMethod("GET");
    Iterator localIterator;
    if ((paramMap != null) && (paramMap.size() != 0))
      localIterator = paramMap.entrySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        localHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091111 Gentoo Firefox/3.5.4");
        boolean bool = this.usingRedirect;
        localHttpURLConnection.setInstanceFollowRedirects(bool);
        setTimeouts(localHttpURLConnection);
        PrintStream localPrintStream = System.out;
        String str1 = "url:" + paramString;
        localPrintStream.println(str1);
        localHttpURLConnection.connect();
        HttpResponse localHttpResponse1 = new HttpResponse();
        HttpResponse localHttpResponse2 = ReadResponse(localHttpURLConnection, localHttpResponse1);
        localHttpURLConnection.disconnect();
        return localHttpResponse1;
      }
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str2 = (String)localEntry.getKey();
      String str3 = (String)localEntry.getValue();
      localHttpURLConnection.addRequestProperty(str2, str3);
    }
  }

  public int getConnectTimeout()
  {
    return this.connectTimeout;
  }

  public int getProxyFlag()
  {
    return this.proxyFlag;
  }

  public int getReadTimeout()
  {
    return this.readTimeout;
  }

  public void imageContentToUpload(OutputStream paramOutputStream, File paramFile)
    throws IOException
  {
    FileInputStream localFileInputStream = new FileInputStream(paramFile);
    String str = paramFile.getName();
    imageContentToUpload(paramOutputStream, localFileInputStream, str);
  }

  public void imageContentToUpload(OutputStream paramOutputStream, InputStream paramInputStream, String paramString)
    throws IOException
  {
    StringBuilder localStringBuilder1 = new StringBuilder();
    StringBuilder localStringBuilder2 = localStringBuilder1.append("--7d4a6d158c9").append("\r\n");
    StringBuilder localStringBuilder3 = localStringBuilder1.append("Content-Disposition: form-data; name=\"pic\"; filename=\"").append(paramString).append("\"\r\n");
    String str = "image/png";
    BufferedInputStream localBufferedInputStream;
    byte[] arrayOfByte2;
    if ((paramString.endsWith(".jpg")) || (paramString.endsWith(".jpeg")))
    {
      str = "image/jpeg";
      StringBuilder localStringBuilder4 = localStringBuilder1.append("Content-Type: ").append(str).append("\r\n\r\n");
      byte[] arrayOfByte1 = localStringBuilder1.toString().getBytes();
      paramOutputStream.write(arrayOfByte1);
      localBufferedInputStream = new BufferedInputStream(paramInputStream);
      arrayOfByte2 = new byte[10240];
    }
    while (true)
    {
      int i = localBufferedInputStream.read(arrayOfByte2);
      if (i == -1)
      {
        localBufferedInputStream.close();
        byte[] arrayOfByte3 = "\r\n".getBytes();
        paramOutputStream.write(arrayOfByte3);
        return;
        if (!paramString.endsWith(".gif"))
          break;
        str = "image/gif";
        break;
      }
      paramOutputStream.write(arrayOfByte2, 0, i);
    }
  }

  public void imageContentToUpload(OutputStream paramOutputStream, String paramString)
    throws IOException
  {
    File localFile = new File(paramString);
    imageContentToUpload(paramOutputStream, localFile);
  }

  public boolean isGZIPValidate()
  {
    return this.usingGZIP;
  }

  public boolean isRedirectValidate()
  {
    return this.usingRedirect;
  }

  public boolean isTimeOutValidate()
  {
    return this.usingTimeOut;
  }

  public void paramToUpload(OutputStream paramOutputStream, String paramString1, String paramString2)
    throws IOException
  {
    StringBuilder localStringBuilder1 = new StringBuilder(10);
    localStringBuilder1.setLength(0);
    StringBuilder localStringBuilder2 = localStringBuilder1.append("--7d4a6d158c9").append("\r\n");
    StringBuilder localStringBuilder3 = localStringBuilder1.append("content-disposition: form-data; name=\"").append(paramString1).append("\"\r\n\r\n");
    StringBuilder localStringBuilder4 = localStringBuilder1.append(paramString2).append("\r\n");
    byte[] arrayOfByte = localStringBuilder1.toString().getBytes();
    paramOutputStream.write(arrayOfByte);
  }

  public HttpResponse post(String paramString, Map<String, String> paramMap)
    throws IOException
  {
    return post(paramString, paramMap, null);
  }

  public HttpResponse post(String paramString, Map<String, String> paramMap1, Map<String, String> paramMap2)
    throws IOException
  {
    return basicRequest(paramString, paramMap1, paramMap2, "POST");
  }

  public void setConnectTimeout(int paramInt)
  {
    this.connectTimeout = paramInt;
  }

  public void setProxyFlag(int paramInt)
  {
    this.proxyFlag = paramInt;
  }

  public void setReadTimeout(int paramInt)
  {
    this.readTimeout = paramInt;
  }

  public HttpResponse upload(String paramString, byte[] paramArrayOfByte, Map<String, String> paramMap)
    throws IOException
  {
    int i = this.proxyFlag;
    HttpURLConnection localHttpURLConnection = buildProxyConnection(paramString, i);
    localHttpURLConnection.setDoOutput(1);
    localHttpURLConnection.setRequestMethod("POST");
    localHttpURLConnection.setUseCaches(0);
    boolean bool = this.usingRedirect;
    localHttpURLConnection.setInstanceFollowRedirects(bool);
    localHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
    localHttpURLConnection.setRequestProperty("Charset", "UTF-8");
    localHttpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=7d4a6d158c9");
    localHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091111 Gentoo Firefox/3.5.4");
    if (this.usingGZIP)
      localHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate");
    Iterator localIterator;
    if ((paramMap != null) && (paramMap.size() != 0))
      localIterator = paramMap.entrySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
      {
        setTimeouts(localHttpURLConnection);
        PrintStream localPrintStream = System.out;
        String str1 = "url:" + paramString;
        localPrintStream.println(str1);
        localHttpURLConnection.connect();
        OutputStream localOutputStream = localHttpURLConnection.getOutputStream();
        BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(localOutputStream);
        DataOutputStream localDataOutputStream = new DataOutputStream(localBufferedOutputStream);
        localDataOutputStream.write(paramArrayOfByte);
        localDataOutputStream.close();
        HttpResponse localHttpResponse1 = new HttpResponse();
        HttpResponse localHttpResponse2 = ReadResponse(localHttpURLConnection, localHttpResponse1);
        localHttpURLConnection.disconnect();
        return localHttpResponse1;
      }
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str2 = (String)localEntry.getKey();
      String str3 = (String)localEntry.getValue();
      localHttpURLConnection.addRequestProperty(str2, str3);
    }
  }

  public void validateGZIP(boolean paramBoolean)
  {
    this.usingGZIP = paramBoolean;
  }

  public void validateRedirect(boolean paramBoolean)
  {
    this.usingRedirect = paramBoolean;
  }

  public void validateTimeOut(boolean paramBoolean)
  {
    this.usingTimeOut = paramBoolean;
  }

  public void writeEndToUpload(OutputStream paramOutputStream)
    throws IOException
  {
    byte[] arrayOfByte = "\r\n--7d4a6d158c9--".getBytes();
    paramOutputStream.write(arrayOfByte);
  }
}

/* Location:           D:\Program Files\eclipse\apktool-r01-3\新浪微博\classes.dex.dex2jar.jar
 * Qualified Name:     sudroid.net.HttpUtil
 * JD-Core Version:    0.6.0
 */